//
//  ViewController.m
//  LottieAnimation
//
//  Created by 李江湖 on 2018/7/6.
//  Copyright © 2018年 李江湖. All rights reserved.
//

#import "ViewController.h"
#import "Lottie.h"
#import "UIImageView+BaseImage.h"
#define WeakObj(obj) __weak typeof(obj) obj##Weak = obj
#define BAKeyWindow [UIApplication sharedApplication].keyWindow
#define BABlackColor [UIColor blackColor] //黑色
//尺寸
#define BAScreenHeight [UIScreen mainScreen].bounds.size.height//屏幕高度
#define BAScreenWidth [UIScreen mainScreen].bounds.size.width//屏幕宽度
@interface ViewController ()
@property (nonatomic, strong) UIImageView *launchMask;
@property (nonatomic, strong) LOTAnimationView *launchAnimation;
@property (nonatomic, strong) LOTAnimationView *loadAnimation;
@end

@implementation ViewController
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if (_launchMask && _launchAnimation) {
        WeakObj(self);
        [_launchAnimation playWithCompletion:^(BOOL animationFinished) {

            [UIView animateWithDuration:0.3 animations:^{
                selfWeak.launchMask.alpha = 0;
            } completion:^(BOOL finished) {
                [selfWeak.launchAnimation removeFromSuperview];
                selfWeak.launchAnimation = nil;
                [selfWeak.launchMask removeFromSuperview];
                selfWeak.launchMask = nil;
            }];
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _launchMask = [UIImageView imageViewWithFrame:self.view.bounds image:[UIImage imageNamed:@"launchAnimationBg"]];
    [self.view addSubview:_launchMask];
    _launchAnimation = [LOTAnimationView animationNamed:@"launchAnimation"];
    _launchAnimation.cacheEnable = NO;
    _launchAnimation.frame = CGRectMake(50, 200, 250, 250);
    _launchAnimation.contentMode = UIViewContentModeScaleToFill;
    _launchAnimation.animationSpeed = 1.2;
    [_launchMask addSubview:_launchAnimation];
    

}
- (IBAction)loadingClick:(UIButton *)sender {

    UIView *bgView = [[UIView alloc] initWithFrame:self.view.bounds];
    bgView.backgroundColor = [BABlackColor colorWithAlphaComponent:0.5];
    bgView.userInteractionEnabled = YES;
    bgView.tag = -9999;
    
    [self.view addSubview:bgView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgClick)];
    [bgView addGestureRecognizer:tap];
    LOTAnimationView *loadAnimation = [LOTAnimationView animationNamed:@"loading"];
    loadAnimation.cacheEnable = NO;
    loadAnimation.frame = CGRectMake(BAScreenWidth / 2 - 375 / 2, BAScreenHeight / 2 - 272 / 2, 375, 272);
    loadAnimation.contentMode = UIViewContentModeScaleToFill;
    loadAnimation.loopAnimation = YES;
    [loadAnimation play];
    
    [bgView addSubview:loadAnimation];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hideGIFHud];
    });
}
-(void)bgClick{
    [self hideGIFHud];
    
}
- (void)hideGIFHud{
    [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == -9999) {
            [obj removeFromSuperview];
            *stop = YES;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
